<template>
  <div class="add-lease-drawer">
    <el-drawer
      :title="
        leaseInfo && leaseInfo.leaseId
          ? '编辑办公租赁与评估信息'
          : '添加办公租赁与评估信息'
      "
      :visible.sync="drawer"
      direction="rtl"
      :wrapperClosable="false"
      :modal-append-to-body="false"
      :show-close="false"
      size="55%"
    >
      <div class="add-property-content">
        <el-form ref="form" :model="form" label-width="150px" size="small">
          <div class="add-property-content-scroll">
            <el-row>
              <el-col :span="12">
                <el-form-item label="租赁性质：" label-width="100%">
                  <el-radio-group v-model="form.leaseNature">
                    <el-radio :label="1">经营性</el-radio>
                    <el-radio :label="0">非经营性</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="评估报告日期："
                  v-if="form.leaseNature === 1"
                >
                  <el-date-picker
                    v-model="form.assessReportDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="租户名称：">
                  <el-input v-model="form.leaseName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="评估价值时点"
                  v-if="form.leaseNature === 1"
                >
                  <el-date-picker
                    v-model="form.assessValueDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="租户联系方式：">
                  <el-input v-model="form.lesseeContact"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" v-if="form.leaseNature === 1">
                <el-form-item
                  label="评估价值金额（元/天/㎡）："
                  label-width="100%"
                >
                  <el-input v-model="form.assessValueAmount"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12" v-if="form.leaseNature === 1">
                <el-form-item label="租赁面积（㎡）：">
                  <el-input v-model="form.leaseArea"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12" v-else-if="form.leaseNature === 0">
                <el-form-item label="使用面积（㎡）：">
                  <el-input v-model="form.leaseArea"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="租赁说明：">
                  <el-input
                    type="textarea"
                    :rows="3"
                    placeholder="请输入内容"
                    v-model="form.leaseInstruction"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="10" v-if="form.leaseNature === 1">
                <el-form-item label="评估报告：" label-width="100%">
                  <el-upload
                    class="upload-demo"
                    action="/admin/common/upload"
                    :headers="headersAuthorization"
                    :on-success="
                      (response, file, fileList) =>
                        handlePgSuccess(response, file, fileList)
                    "
                    :on-remove="
                      (file, fileList) => handleRemove(file, fileList, 'pg')
                    "
                    multiple
                    :limit="3"
                    :file-list="pgbgList"
                  >
                    <el-button size="mini" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col
                :span="22"
                class="upload-img-opt"
                v-if="form.leaseNature === 1"
              >
                <p>租金 ：</p>
                <el-table :data="form.leaseRentList" border>
                  <el-table-column
                    prop="id"
                    label="序号"
                    width="55"
                    align="center"
                  >
                    <template slot-scope="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column label="租金（万元/每年）" align="center">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.rentAmount"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="上浮（%）" align="center" width="100">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.rentFloating"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="是否缴纳租金" align="center">
                    <template slot-scope="scope">
                      <el-select
                        v-model="scope.row.rentPayFlag"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in rentPayFlagList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="租金缴纳日期" align="center">
                    <template slot-scope="scope">
                      <el-date-picker
                        v-model="scope.row.rentPayDate"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%"
                        v-if="scope.row.rentPayFlag === 1"
                      >
                      </el-date-picker>
                    </template>
                  </el-table-column>
                  <el-table-column label="租金到期日期" align="center">
                    <template slot-scope="scope">
                      <el-date-picker
                        v-model="scope.row.payRentDate"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%"
                        v-if="scope.row.rentPayFlag === 1"
                      >
                      </el-date-picker>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" width="60">
                    <template slot="header" slot-scope="scope">
                      <i
                        class="el-icon-plus"
                        style="cursor: pointer"
                        @click="addClick"
                      ></i>
                    </template>
                    <template slot-scope="scope">
                      <i
                        class="el-icon-minus"
                        style="cursor: pointer"
                        @click="deleteClick(scope.$index)"
                      ></i>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>

              <el-col :span="8">
                <el-form-item label="其他附件：" label-width="100%">
                  <el-upload
                    class="upload-demo"
                    action="/admin/common/upload"
                    :headers="headersAuthorization"
                    :on-success="
                      (response, file, fileList) =>
                        handleQtSuccess(response, file, fileList)
                    "
                    :on-remove="
                      (file, fileList) => handleRemove(file, fileList, 'qt')
                    "
                    multiple
                    :limit="3"
                    :file-list="qtfjList"
                  >
                    <el-button size="mini" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row v-if="form.leaseNature === 1">
              <el-col :span="12" class="upload-img-opt">
                <p>押金 ：</p>
                <el-table :data="form.leaseDepositList" border>
                  <el-table-column
                    prop="id"
                    label="序号"
                    width="55"
                    align="center"
                  >
                    <template slot-scope="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="押金缴纳日期"
                    width="160"
                    align="center"
                  >
                    <template slot-scope="scope">
                      <el-date-picker
                        v-model="scope.row.payDepositDate"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </template>
                  </el-table-column>
                  <el-table-column label="押金（万元）" align="center">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.depositAmount"></el-input>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="租赁合同签订日期：">
                  <el-date-picker
                    v-model="form.leaseContractBeginDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="租赁合同终止日期：">
                  <el-date-picker
                    v-model="form.leaseContractEndDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="租赁协议：" label-width="100%">
                  <el-upload
                    class="upload-demo"
                    action="/admin/common/upload"
                    :headers="headersAuthorization"
                    :on-success="
                      (response, file, fileList) =>
                        handleZlSuccess(response, file, fileList)
                    "
                    :on-remove="
                      (file, fileList) => handleRemove(file, fileList, 'zl')
                    "
                    multiple
                    :limit="3"
                    :file-list="zlxyList"
                  >
                    <el-button size="mini" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-form-item class="opt-cla">
            <el-button type="primary" @click="onSubmit">
              <template v-if="leaseInfo && leaseInfo.leaseId"> 编辑 </template>
              <template v-else>添加</template></el-button
            >
            <el-button @click="cancel()">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { addLease, editLease } from "@/api/shops";
import { getToken } from "@/utils/auth";
export default {
  props: {
    drawer: {
      type: Boolean,
      default: false,
    },
    leaseInfo: {},
  },
  data() {
    return {
      form: {
        leaseNature: 1,
        propertyUnitId: undefined,
        leaseName: undefined,
        lesseeContact: undefined,
        leaseInstruction: undefined,
        leaseArea: undefined,
        leaseContractBeginDate: undefined,
        leaseContractEndDate: undefined,
        assessReportDate: undefined,
        assessValueDate: undefined,
        assessValueAmount: undefined,
        assessReportAttachIds: undefined,
        leaseContractAttachIds: undefined,
        otherAttachIds: undefined,
        leaseRentList: [
          {
            payRentDate: "",
            rentPayDate: "",
            rentAmount: "",
            rentFloating: "",
            rentPayFlag: 0,
          },
        ],

        leaseDepositList: [
          {
            payDepositDate: "",
            depositAmount: "",
          },
        ],
      },
      rentPayFlagList: [
        {
          value: 1,
          label: "是",
        },
        {
          value: 0,
          label: "否",
        },
      ],
      qtfjList: [], // 其他附件
      zlxyList: [], //租赁协议
      pgbgList: [], // 评估报告
      headersAuthorization: null,
    };
  },
  watch: {
    leaseInfo: {
      handler(val) {
        // 编辑
        if (val && val.leaseId) {
          this.form = Object.assign({}, val);
          if (
            this.form.assessReportAttachList &&
            this.form.assessReportAttachList.length > 0
          ) {
            this.pgbgList = this.form.assessReportAttachList.map((v) => {
              return {
                name: v.originalAttName,
                url: v.attUrl,
              };
            });
          }
          if (
            this.form.leaseContractAttachList &&
            this.form.leaseContractAttachList.length > 0
          ) {
            this.zlxyList = this.form.leaseContractAttachList.map((v) => {
              return {
                name: v.originalAttName,
                url: v.attUrl,
              };
            });
          }
          if (
            this.form.otherAttachList &&
            this.form.otherAttachList.length > 0
          ) {
            this.qtfjList = this.form.otherAttachList.map((v) => {
              return {
                name: v.originalAttName,
                url: v.attUrl,
              };
            });
          }

          console.log(this.form);
        }
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    clear() {
      this.form = {
        leaseNature: 1,
        propertyUnitId: undefined,
        leaseName: undefined,
        lesseeContact: undefined,
        leaseInstruction: undefined,
        leaseArea: undefined,
        leaseContractBeginDate: undefined,
        leaseContractEndDate: undefined,
        assessReportDate: undefined,
        assessValueDate: undefined,
        assessValueAmount: undefined,
        assessReportAttachIds: undefined,
        leaseContractAttachIds: undefined,
        otherAttachIds: undefined,
        leaseRentList: [
          {
            payRentDate: "",
            rentPayDate: "",
            rentAmount: "",
            rentFloating: "",
            rentPayFlag: 0,
          },
        ],
        leaseDepositList: [
          {
            payDepositDate: "",
            depositAmount: "",
          },
        ],
      };
      this.qtfjList = []; // 其他附件
      this.zlxyList = []; //租赁协议
      this.pgbgList = []; // 评估报告
    },
    cancel() {
      this.$emit("update:drawer", false);
      this.clear();
    },

    init() {
      // 获取用于上传的token
      this.headersAuthorization = {
        Authorization: "Bearer " + getToken(),
      };
    },

    // 租赁协议成功
    handleZlSuccess(res, file, fileList) {
      if (res.code != 200) {
        this.$message.error("上传失败");
        return;
      }
      if (fileList.length > 0) {
        let target = fileList.map((item) => item.response.data.attId);
        if (target.length > 0) {
          this.form.leaseContractAttachIds = target.join(",");
        }
      } else {
        this.form.leaseContractAttachIds = "";
      }
    },
    // 评估报告成功
    handlePgSuccess(res, file, fileList) {
      if (res.code != 200) {
        this.$message.error("上传失败");
        return;
      }
      if (fileList.length > 0) {
        let target = fileList.map((item) => item.response.data.attId);
        if (target.length > 0) {
          this.form.assessReportAttachIds = target.join(",");
        }
      } else {
        this.form.assessReportAttachIds = "";
      }
    },
    // 其他附件成功
    handleQtSuccess(res, file, fileList) {
      if (res.code != 200) {
        this.$message.error("上传失败");
        return;
      }
      if (fileList.length > 0) {
        let target = fileList.map((item) => item.response.data.attId);
        if (target.length > 0) {
          this.form.otherAttachIds = target.join(",");
        }
      } else {
        this.form.otherAttachIds = "";
      }
    },

    handleRemove(file, fileList, type) {
      switch (type) {
        // 租赁协议
        case "zl":
          if (fileList.length > 0) {
            let zlTarget = fileList.map((item) => item.response.data.attId);
            if (zlTarget.length > 0) {
              this.form.leaseContractAttachIds = zlTarget.join(",");
            }
          } else {
            this.form.leaseContractAttachIds = "";
          }
          break;
        // 其他附件
        case "qt":
          if (fileList.length > 0) {
            let qtTarget = fileList.map((item) => item.response.data.attId);
            if (qtTarget.length > 0) {
              this.form.otherAttachIds = qtTarget.join(",");
            }
          } else {
            this.form.otherAttachIds = "";
          }
          break;
        // 评估报告
        case "pg":
          if (fileList.length > 0) {
            let pgTarget = fileList.map((item) => item.response.data.attId);
            if (pgTarget.length > 0) {
              this.form.assessReportAttachIds = pgTarget.join(",");
            }
          } else {
            this.form.assessReportAttachIds = "";
          }
          break;
      }
    },
    addClick() {
      this.form.leaseRentList.push({
        payRentDate: "",
        rentPayDate: "",
        rentAmount: "",
        rentFloating: "",
        rentPayFlag: 0,
      });
    },
    deleteClick(index) {
      this.form.leaseRentList.splice(index, 1);
    },
    /**新增or编辑 */
    onSubmit() {
      let rentPayFlagArr = this.form.leaseRentList.filter(
        (v) => v.rentPayFlag === 1 && (!v.payRentDate || !v.rentPayDate)
      );
      if (rentPayFlagArr && rentPayFlagArr.length > 0) {
        this.$message.error("请填写租金缴纳日期与租金到期日期");
        return;
      }
      this.form.leaseRentList.forEach((v) => {
        if (v.payRentDate) {
          v.payRentDate = this.$moment(v.payRentDate).format("YYYY-MM-DD");
        }
        if (v.rentPayDate) {
          v.rentPayDate = this.$moment(v.rentPayDate).format("YYYY-MM-DD");
        }
      });
      this.form.leaseDepositList.forEach((v) => {
        if (v.payDepositDate) {
          v.payDepositDate = this.$moment(v.payDepositDate).format(
            "YYYY-MM-DD"
          );
        }
      });

      if (this.form && this.form.leaseId) {
        let params = {
          ...this.form,
        };
        editLease(params).then((res) => {
          if (res.code == 200) {
            this.$message.success("编辑成功");
            this.$emit("update:drawer", false);
            this.clear();
            this.$emit("childBgUnitEvent");
          }
        });
      } else {
        this.form.propertyUnitId = this.$route.query.propertyUnitId;
        let params = {
          ...this.form,
        };
        addLease(params).then((res) => {
          if (res.code == 200) {
            this.$message.success("新增成功");
            this.$emit("update:drawer", false);
            this.clear();
            this.$emit("childBgUnitEvent");
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.add-lease-drawer {
  .add-property-content {
    width: 100%;
    .add-property-content-scroll {
      height: calc(100vh - 112px);
      overflow-y: auto;
    }
  }
  .upload-img-opt {
    display: flex;
    flex-direction: column;
    // margin-top: -25px;
    .el-upload__text {
      font-size: 14px;
      font-style: normal;
      line-height: 0;
      padding-top: 10px;
    }
    .img-content {
      flex: 1;
      background: #fafafa;
      margin-left: 6px;
    }
  }
  .upload-quanz {
    width: 90%;
    height: 120px;
    background: #fafafa;
    font-size: 14px;
    .fcz {
      display: flex;
      align-items: center;
      margin: 15px 0 0 15px;
    }
    .tdz {
      display: flex;
      align-items: center;
      margin: 0 0 0 15px;
    }
  }
  .type-content {
    width: 98%;
    background-color: #fafafa;
    padding: 0 20px 20px;
    height: auto;
    // overflow: auto;
    .add-type {
      width: 84.5%;
      height: 30px;
      line-height: 30px;
      text-align: center;
      color: #dcdcdc;
      border: 1px dotted #dcdcdc;
      background: #fff;
      cursor: pointer;
    }
    tr {
      width: 100%;
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      th {
        text-align: center;
        font-size: 14px;
        color: #505a69;
        padding: 10px 0;
        &:nth-child(1) {
          width: 35%;
        }
        &:nth-child(2) {
          width: 50%;
        }
      }
      td {
        &:nth-child(1) {
          width: 35%;
        }
        &:nth-child(2) {
          width: 55%;
        }
        &:nth-child(3) {
          width: 10%;
          img {
            width: 24px;
            height: 24px;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
<style lang="scss">
.add-lease-drawer {
  .el-drawer__header {
    background-color: #fafafa;
    padding: 0;
    color: #000;
    margin-bottom: 5px;
  }
  .quanType {
    .el-form-item__label {
      width: 90px !important;
    }
  }
  .el-form-item__label {
    color: #000;
  }
  header#el-drawer__title {
    padding: 10px;
  }
  .el-form-item__content {
    display: contents;
  }
  .el-col {
    margin-left: 20px;
  }
  .el-form-item__label {
    text-align: left;
  }
  .el-form-item--small.el-form-item {
    margin-bottom: 10px;
  }
  .el-upload--picture-card {
    background-color: #fafafa;
    display: flex;
    flex-direction: column;
    padding-top: 40px;
    i {
      font-size: 35px;
    }
  }
  .opt-cla {
    margin: 20px;
    .el-button--default,
    .el-button--primary {
      width: 95px;
      height: 32px;
      padding: 0;
    }
  }
}
</style>